/**
 * 
 */
package at.ac.tuwien.ifs.calendit.basis.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.dao.IMeetingsAnfrage;
import at.ac.tuwien.ifs.calendit.domain.MeetingsAnfrage;


public class MaDetailsModel extends AbstractTableModel{
        /**
	 * 
	 */
	private static final long serialVersionUID = 4066800547151527497L;
		private List <MeetingsAnfrage> meetingsanfrage = null;
        private MeetingsAnfrage t;
        private XmlBeanFactory xbf = null;
        private  IMeetingsAnfrage maDao = null;
        private RaumTableModel raumTM;
        private TODOTableModel todoTM;
        private ClassPathResource res = null;
       

        private final String[] colnames = {"Bezeichnung","Datum", "Beginn", "Ende","Raum","Aufgabe"};
        
        public MaDetailsModel(){
                super();
                initDAO();
                getMAData();
        }
        private void getMAData(){
        	MeetingsAnfrage temp= new MeetingsAnfrage();
        	temp.setTeilnehmerid(Constants.getUserId());
        	temp.setErstellerid(Constants.getUserId());
                meetingsanfrage = maDao.getAll(temp);
                }
        
        /**
		 * Returns a single meetingsanfrage out of this TableModel.
		 * 
		 * @param i
		 *            The index of the desired meetingsanfrage.
		 * @return The meetingsanfrage at position <c>i</c>.
		 * @see #getmeetingsanfrage()
		 */
		public MeetingsAnfrage getMeetingsanfrageAt(int i) {
			return meetingsanfrage.get(i);
		}
		
		public boolean deleteMA(int id){
			return maDao.delete(id);
		}

        public int getColumnCount(){
                return colnames.length;
        }
        public int getRowCount(){
                return meetingsanfrage.size();
        }
        public String getColumnName(int c){
                return colnames[c];
        }
        public MeetingsAnfrage getTerminToTermintyp(int typ){
                t= maDao.getById(typ);
                fireTableDataChanged();
                return t;
        }
        public Object getValueAt(int row, int col){
                MeetingsAnfrage t = meetingsanfrage.get(row);
                switch(col){
                case 0: 
                        return t.getBezeichnung();
                case 1:
                        return t.getDatum();
                case 2: 
                        return t.getBeginnzeit();
                case 3:
                        return t.getEndzeit();
                case 4:
                		return raumTM.getRaumById(t.getRaumid()).getRaumname();
//                	    return t.getRaumid();
                case 5:
                        return todoTM.getTodoByID(t.getAgendaid()).getBezeichnung();         
                default:
                        return null;
                                }
        }
        public MeetingsAnfrage getMeetingsAnfrageToId(int i){
                return meetingsanfrage.get(i);
        }
        public void reload(){
                getMAData();
                fireTableDataChanged();
        }
        private void initDAO(){

              res = new ClassPathResource(Constants.SPRINGBEANS);

            res = new ClassPathResource(Constants.SPRINGBEANS);

                xbf = new XmlBeanFactory(res);
                maDao = (IMeetingsAnfrage) xbf.getBean("MeetingsAnfrageDAO");
                raumTM= new RaumTableModel("ID");
                todoTM= new TODOTableModel("aufgabeId");
        	}
        
        public MeetingsAnfrage insert(MeetingsAnfrage ma){
        	return maDao.add(ma);
        }		
        
        public int getMaxOfKennung(){
    		return maDao.getMaxOfKennung();
    	}
        
        public List<MeetingsAnfrage> getMAByKennung(int id){
        	return maDao.getMeetingsAnfrageByKennung(id);
        }

        

        
    	

}
